<?php $Gateway = config('workman.Gateway');?>
<a onclick="sendMessage()">触发</a> account:0566    password:123456
<br>
<a onclick="sendRequest()">提交Request参数</a>
<br><input type="text" id="f" value="/login" placeholder="默认方法">
<br><button onclick="addInput()">新建input</button>
<span id="addDiv"></span>
{{--<a onclick="getAllPar()">查看所有参数</a>--}}
<script>
    var ws = new WebSocket("ws://{{$Gateway['ip']}}:{{$Gateway['port']}}");
    var addDiv = document.getElementById("addDiv");
    ws.onopen = function (e) {
        console.log('Connection to server opened');
        setInterval(function () {
            ws.send(JSON.stringify({type:'ping'}));
        },20000);
    }
    function sendMessage() {
        let data = getAllPar();
        data['f'] = document.getElementById('f').value;
        ws.send(JSON.stringify(data));
    }

    function sendRequest() {
        ws.send(JSON.stringify(params));
    }

    let keyinit = 1;
    function addInput() {
        var br = document.createElement("br");
        addDiv.appendChild(br);
        //添加text ，存放指标权重
        var input = document.createElement("input");
        input.setAttribute('type', 'text');
        input.setAttribute('id', 'key'+keyinit);
        input.setAttribute('placeholder', "键(key)");
        input.setAttribute('class', "childKeys");
        addDiv.appendChild(input);
        var input1 = document.createElement("input");
        input1.setAttribute('type', 'text');
        input1.setAttribute('id', 'value'+keyinit);
        input1.setAttribute('placeholder', "值(value)");
        input1.setAttribute('class', "childValues");
        addDiv.appendChild(input1);
        keyinit++;
    }

    function getAllPar() {
        let arr = {};
        let childKeys = document.getElementsByClassName('childKeys');
        let childValues = document.getElementsByClassName('childValues');
        for(let i = 0;i<childKeys.length;i++) {
            let key = childKeys[i].value;
            let value = childValues[i].value;
            arr[key] = value;
        }
        return arr;
    }

    function combin(a,b){
        for(let i in b){
            a[i] = b[i]
        }
        return a;
    }

    var params = (function oneValues() {
        var query = location.search.substr(1)
        query = query.split('&')
        var params = {}
        for (let i = 0; i < query.length; i++) {
            let q = query[i].split('=')
            if (q.length === 2) {
                params[q[0]] = q[1]
            }
        }
        return params  //返回?号后面的参数名与参数值的数组
    }());
</script>
